package netnotifier.notification;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;


public class TCPPacket extends Packet
{
	public int tcpOffset=0;
	
	public int sourcePort=0;
	public int destinationPort=0;
	
	public long sequenceNumber=0;
	public long acknowledgementNumber=0;
	
	public int dataOffset=0;
	public int dataLength=0;
	public byte[] tcpData;
	
	public int flags=0;
	public ArrayList<String> flagList=new ArrayList<String>();
	
	public TCPPacket(byte[] rawData, int offset, int tcpSize)
	{
		tcpOffset=offset;
		
		sourcePort|=(rawData[offset+0]&0xff);
		sourcePort<<=8;
		sourcePort|=(rawData[offset+1]&0xff);
		
		destinationPort|=(rawData[offset+2]&0xff);
		destinationPort<<=8;
		destinationPort|=(rawData[offset+3]&0xff);
		
		sequenceNumber|=(rawData[offset+4]&0xff);
		sequenceNumber<<=8;
		sequenceNumber|=(rawData[offset+5]&0xff);
		sequenceNumber<<=8;
		sequenceNumber|=(rawData[offset+6]&0xff);
		sequenceNumber<<=8;
		sequenceNumber|=(rawData[offset+7]&0xff);
		
		acknowledgementNumber|=(rawData[offset+8]&0xff);
		acknowledgementNumber<<=8;
		acknowledgementNumber|=(rawData[offset+9]&0xff);
		acknowledgementNumber<<=8;
		acknowledgementNumber|=(rawData[offset+10]&0xff);
		acknowledgementNumber<<=8;
		acknowledgementNumber|=(rawData[offset+11]&0xff);
		
		dataOffset|=(rawData[offset+12]&0xf0);
		dataOffset>>=2;
		dataLength=tcpSize-dataOffset;
		
		tcpData=Arrays.copyOfRange(rawData,offset+dataOffset,offset+tcpSize);

		flags=rawData[offset+13]&0xff;
		
		if((flags&0x1)!=0)flagList.add("fin");
		if((flags&0x2)!=0)flagList.add("syn");
		if((flags&0x4)!=0)flagList.add("rst");
		if((flags&0x8)!=0)flagList.add("psh");
		if((flags&0x10)!=0)flagList.add("ack");
		if((flags&0x20)!=0)flagList.add("urg");
		if((flags&0x40)!=0)flagList.add("ece");
		if((flags&0x80)!=0)flagList.add("cwr");
	}
	
	public static String[] defaultFields= new String[]{"sourcePort", "destinationPort","flagList","sequenceNumber","acknowledgementNumber"};
}

